/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tree.TreeBean;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * @author Andree
 */
public class ResultTree extends HttpServlet {
    
    private net.minidev.json.JSONArray data;
    
    private TreeBean tb;
    
    public static Map resultTreeMap = new HashMap();
    
    

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String root = request.getParameter("root");
        
        try {
           if(root.equals("source"))
                        {
                            //if isNewTab_OR_TabSwitched is false
                            if(!Controller.isNewTab_OR_TabSwitched)
                            {                              
                                data = new net.minidev.json.JSONArray();
                                //For Result Root Node
                                tb = new TreeBean();
                                tb.setId("Result");
                                tb.setText("Result");
                                tb.setClasses("Result");
                                tb.setHasChildren(true);
                                tb.setExpanded(false);

                                data.add(tb);

                                resultTreeMap.put(Controller.tabNumber+"_"+0, data);
                                resultTreeMap.put(Controller.tabNumber+"_"+1, getSecondLevelData());
                                
                                response.setContentType("application/json");
                                response.setCharacterEncoding("UTF-8");
                                response.getWriter().print(data);
                            }
                            else if(Controller.isNewTab_OR_TabSwitched)
                            {
                                net.minidev.json.JSONArray tempData = new net.minidev.json.JSONArray();
                                tempData = (net.minidev.json.JSONArray)resultTreeMap.get(Controller.tabNumber+"_"+0);
                                
                                if(tempData != null)
                                {
                                    response.setContentType("application/json");
                                    response.setCharacterEncoding("UTF-8");
                                    response.getWriter().print(tempData);
                                }
                                else
                                {
                                    data = new net.minidev.json.JSONArray();
                                    //For Result Root Node
                                    tb = new TreeBean();
                                    tb.setId("No Result");
                                    tb.setText("No Result");
                                    tb.setClasses("No Result");
                                    tb.setHasChildren(false);
                                    tb.setExpanded(false);

                                    data.add(tb);
                                    
                                    response.setContentType("application/json");
                                    response.setCharacterEncoding("UTF-8");
                                    response.getWriter().print(data);
                                }
                            }                               
                        }
            else if(root.equals("Result"))
            {
                     //if isNewTab_OR_TabSwitched is false
                            if(!Controller.isNewTab_OR_TabSwitched)
                            {
                                data = getSecondLevelData();

                                response.setContentType("application/json");
                                response.setCharacterEncoding("UTF-8");
                                response.getWriter().print(data);
                            }
                            else if(Controller.isNewTab_OR_TabSwitched)
                            {
                                net.minidev.json.JSONArray tempData = new net.minidev.json.JSONArray();
                                tempData = (net.minidev.json.JSONArray)resultTreeMap.get(Controller.tabNumber+"_"+1);
                                
                                Controller.isNewTab_OR_TabSwitched = false;
                                response.setContentType("application/json");
                                response.setCharacterEncoding("UTF-8");
                                response.getWriter().print(tempData);
                            }
            }
             
        } 
        catch(Exception e)
        {
            e.printStackTrace();
        }    
    }
    
    public net.minidev.json.JSONArray getSecondLevelData()
    {
        net.minidev.json.JSONArray data2 = new net.minidev.json.JSONArray();
        
        tb = new TreeBean();
                            tb.setId("1");
                            tb.setText("&ltSOURCE&gt "+Controller.resultSelectedDataSource+"");
                            tb.setClasses("1");
                            tb.setHasChildren(false);
                            tb.setExpanded(false);

                            data2.add(tb);
                            
                            tb = new TreeBean();
                            tb.setId("2");
                            tb.setText("&ltOUTPUT&gt "+Controller.newQueryDraggedNode+"");
                            tb.setClasses("2");
                            tb.setHasChildren(false);
                            tb.setExpanded(false);

                            data2.add(tb);
                            
                            tb = new TreeBean();
                            tb.setId("3");
                            tb.setText("&ltTOTAL COUNT&gt "+Controller.totalCount+"");
                            tb.setClasses("3");
                            tb.setHasChildren(false);
                            tb.setExpanded(false);

                            data2.add(tb);
        
        return data2;
    }        

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}